//package com.migu.controller;
//
//import SysBackApi;
//import SysBackApiRepository;
//import com.google.common.base.Optional;
//import com.google.common.collect.Maps;
//import io.swagger.models.HttpMethod;
//import io.swagger.models.Operation;
//import io.swagger.models.Path;
//import io.swagger.models.Swagger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestMethod;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//import springfox.documentation.service.Documentation;
//import springfox.documentation.spring.web.DocumentationCache;
//import springfox.documentation.spring.web.json.Json;
//import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger2.mappers.ServiceModelToSwagger2Mapper;
//
//import java.util.*;
//
//
///**
// * @Description TODO :
// * @Author: zj
// * @Date: 15:51 2019/3/22
// * @Version 1.0
// **/
//@RequestMapping(path = "/api/swagger")
//@Controller
//public class swaggerController {
//
//    @Autowired
//    DocumentationCache documentationCache;
//    @Autowired
//    ServiceModelToSwagger2Mapper mapper;
//    @Autowired
//    SysBackApiRepository backendApiRepository;
//
//    @RequestMapping(
//            value = "/api/updateApi", method = RequestMethod.GET)
//    @ResponseBody
//    public ResponseEntity<Json> updateApi(
//            @RequestParam(value = "group", required = false) String swaggerGroup) {
//        // 加载已有的api
//        Map<String, Boolean> apiMap = Maps.newHashMap();
//        List<SysBackApi> apis = backendApiRepository.findAllApi();
//        Set<String> apiSet = new HashSet<>();
//        apis.forEach(e -> apiSet.add(e.getTag()));
//        Map<String, List<SysBackApi>> listMap = Maps.newHashMap();
//        for (String s : apiSet) {
//            List<SysBackApi> sysBackApis = new ArrayList<>();
//            for (SysBackApi api : apis) {
//                if (api.getTag().equals(s)) {
//                    sysBackApis.add(api);
//                }
//            }
//            listMap.put(s, sysBackApis);
//        }
//        apis.stream().forEach(api -> apiMap.put(api.getPath() + api.getMethod(), true));
//        // 获取swagger
//        String groupName = Optional.fromNullable(swaggerGroup).or(Docket.DEFAULT_GROUP_NAME);
//        Documentation documentation = documentationCache.documentationByGroup(groupName);
//        if (documentation == null) {
//            return new ResponseEntity<Json>(HttpStatus.NOT_FOUND);
//        }
//        Swagger swagger = mapper.mapDocumentation(documentation);
//
//        // 加载到数据库
//        for (Map.Entry<String, Path> item : swagger.getPaths().entrySet()) {
//            String path = item.getKey();
//            Path pathInfo = item.getValue();
//            createApiIfNeeded(apiMap, path, pathInfo.getGet(), HttpMethod.GET.name());
//            createApiIfNeeded(apiMap, path, pathInfo.getPost(), HttpMethod.POST.name());
//            createApiIfNeeded(apiMap, path, pathInfo.getDelete(), HttpMethod.DELETE.name());
//            createApiIfNeeded(apiMap, path, pathInfo.getPut(), HttpMethod.PUT.name());
//        }
//        return new ResponseEntity<Json>(HttpStatus.OK);
//    }
//    //其中createApiIfNeeded，先判断下是否存在，不存在的则新增：
//    private void createApiIfNeeded(Map<String, Boolean> apiMap, String path, Operation operation, String method) {
//        if (operation == null) {
//            return;
//        }
//        if (!apiMap.containsKey(path + method)) {
//            apiMap.put(path + method, true);
//            SysBackApi backApi = new SysBackApi();
//            backApi.setMethod(method);
//            backApi.setPath(path);
//            backApi.setTag(operation.getTags().get(0));
//            backApi.setSummary(operation.getSummary());
//            // 保存
//            this.backendApiRepository.saveApi(backApi);
//        }
//    }
//
//
//}
